#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "linkedlist.h"
#include "fun.h"

//为联系人添加一个结点空间
LXR * alloc_one_people()
{
        LXR * load = NULL;

	load = malloc(sizeof(LXR));
	assert(load != NULL);
	load->prev = NULL;
	load->next = NULL;

	return load;
}

//在一个分组下创建一个联系人的空链表
LXR * creat_people_list()
{
	LXR * head = NULL;

	head = alloc_one_people();	

	return head;
}

//定位到最后一个结点
LXR * list_end(LXR * head)
{
	LXR * load = head;

	while(load->next != NULL)
	{
		load = load->next;
	}

	return load;
}

//判断电话号码是否已存在
int isSameL(LXR * head, char * newPhoneNum)
{
	LXR * load = head;

	while(load->next != NULL)
	{
		load = load->next;
		if(strcmp(load->phoneNum, newPhoneNum) == 0)
		{
			return 0;
		}
	}

	return 1;
}

//检测手机号码格式是否正确
int is_check_phoneNum(char * str)
{
	int i, len;

	len = strlen(str);
	if(str[0] != '1' || len != 11)
	{
		return 0;
	}
	for(i = 1; i < 11; i++)
	{
		if(str[i] < '0' || str[i] > '9')
		{
			return 0;
		}
	}

	return 1;
}

//添加一个新的联系人
LXR * add_new_people(LXR * head, char * newName, char * newPhoneNum)
{
	LXR * new = NULL;
	LXR * load = list_end(head);

	new = alloc_one_people();
	if(isSameL(head, newPhoneNum) == 1)
	{
	 	strcpy(new->name, newName);
		strcpy(new->phoneNum, newPhoneNum);
		new->prev = load;
		load->next = new;
	}
 	else
 	{
 		printf("该联系人已存在！！\n\n");
 		free(new);
 	}

	return head;
}

//打印一位联系人信息
void print_one_people(LXR * load)
{
	printf("\t%-10s\t%-12s\n", load->name, load->phoneNum);
}

//打印所有联系人信息
void print_all_people(LXR * head)
{
	LXR * load = head;
	int len = 0;
	char ch = 'y';

	while(load->next != NULL)
	{
		load = load->next;
		len++;
		if(ch == 'y' || ch == 'Y')
		{
			print_one_people(load);		
		}
		if(len % 10 == 0)
		{
			printf("是否继续显示下一页\n");
			ch = isSure();
		}		
	}

}

//修改一个联系人信息
void modify_one_people(LXR * pload, char * name)
{
	char ch, newName[20], newPhoneNum[20];

	if(pload != NULL)
	{			
		printf("您想要修改姓名吗？\n");
		ch = isSure();
		if(ch == 'y' || ch == 'Y')
		{
			printf("请输入新和姓名\n");
			scanf("%s", newName);
			strcpy(pload->name, newName);
		}
		printf("您要修改电话号码吗？\n");
		ch = isSure();
		if(ch == 'y' || ch == 'Y')
		{
			while(1)
			{
				printf("请输入新的电话号码\n");
				fflush(stdin);
				scanf("%s", newPhoneNum);
				if(is_check_phoneNum(newPhoneNum) == 1)
				{
					while(strcmp(pload->phoneNum, newPhoneNum) == 0)
					{
						printf("新的号码不能与原号码相同\n");
						printf("请重新输入\n");
						scanf("%s", newPhoneNum);
					}
					strcpy(pload->phoneNum, newPhoneNum);
					break;				
				}
				printf("请输入正确的电话号码\n");
			}				
		}
	}
	else
	{
		printf("%s 不存在!!\n", name);
	}
	printf("修改成功！\n\n");	
}

//查找一个联系人
LXR * sel_one_people(LXR * head, char * str)
{
	LXR * load = head;
	
	while(load->next != NULL)
	{
		load = load->next;
		if(strcmp(load->name, str) == 0)
		{
			return load;
		}
	}

	return NULL;
}

//删除一个联系人
void del_one_people(LXR * load, char * str)
{
	LXR * pre_load = load->prev;

	pre_load->next = load->next;

	free(load);
}

//获取分组下的人数
int get_people_length(LXR * load)
{
	int len = 0;

	while(load->next != NULL)
	{
		load = load->next;
		len++;
	}

	return len;
}

//冒泡排序联系人
LXR * bubble_sort_people(LXR * head, int len)
{
	int i, j;
	LXR * r;
	LXR * p;
	LXR * q;

	for(i = 0; i < len-1; i++)
	{	
		r = head;
		for(j = 0; j < len-1-i; j++)
		{	
			p = r->next;
			q = p->next;
			if(strcmp(p->name, q->name) > 0)
			{
				p->next = q->next;
				q->next = p;
				r->next = q;	
			}
			r = r->next;
		}	
	}
	return head;
}

//将一个分组下的联系人移动到别一个分组下
void move_group(GROUP * oload, GROUP * nload)
{
	LXR * pload = oload->h;

	while(pload->next != NULL)
	{
		pload = pload->next;
		add_new_people(nload->h, pload->name, pload->phoneNum);	
	}
}